package com.ksy.common.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ksy.common.R;
import com.ksy.common.activity.CommonBaseActivity;
import com.ksy.common.utils.DateUtil;
import com.ksy.common.utils.LogTool;
import com.ksy.common.view.HeadBar;


/**
 * Created by Ksy.
 */

public class CommonBaseFragment extends Fragment {
    private Toast toast;
    protected CommonBaseActivity activity;
    private ProgressDialog progressDialog;
    private long dialogStartShowTime;
    private HeadBar baseHeadBar;
    protected Handler handler = new Handler(Looper.getMainLooper());

    protected boolean isEmpty(String msg) {
        return TextUtils.isEmpty(msg);
    }

    protected boolean isNotEmpty(String msg) {
        return !TextUtils.isEmpty(msg);
    }

    protected boolean isSame(String msgL, String msgR) {
        return TextUtils.equals(msgL, msgR);
    }

    protected boolean isNotSame(String msgL, String msgR) {
        return !TextUtils.equals(msgL, msgR);
    }

    protected void destroyActivity() {
        if (activity != null) {
            activity.destroyActivity();
        }
    }

    protected void jump2Activity(Class cls) {
        if (cls != null) {
            Intent intent = new Intent(getContext(), cls);
            startActivity(intent);
        }
    }

    protected boolean isShowHeadBar() {
        return false;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.common_fragment_base, container, false);
        LinearLayout baseRootLayout = inflate.findViewById(R.id.baseFragmentRootLayout);
        baseHeadBar = inflate.findViewById(R.id.baseFragmentHeadBar);
        View baseVLine = inflate.findViewById(R.id.baseFragmentVLine);
        if (isShowHeadBar()) {
            baseHeadBar.setVisibility(View.VISIBLE);
            baseVLine.setVisibility(View.VISIBLE);
        } else {
            baseHeadBar.setVisibility(View.GONE);
            baseVLine.setVisibility(View.GONE);
        }
        View view = onCreateView(inflater);
        if (view != null) {
            baseRootLayout.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        return inflate;
    }

    protected HeadBar getHeadBar() {
        return baseHeadBar;
    }

    protected View onCreateView(LayoutInflater inflater) {
        return null;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (CommonBaseActivity) context;
    }

    protected void showLog(Object o) {
        LogTool.e("Log->  " + o.toString());
    }

    protected void showToast(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    @Override
    public void onDestroy() {
        if (toast != null) {
            toast.cancel();
        }
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }


    protected void showDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getContext());
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(true);
            progressDialog.setMessage("请稍后...");
        }
        dialogStartShowTime = DateUtil.getCurrentTimeInMillis();
        progressDialog.show();
    }

    protected void hideDialog() {
        long rangeTime = DateUtil.getCurrentTimeInMillis() - dialogStartShowTime;
        if (rangeTime <= 1000) {
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (progressDialog != null) {
                        progressDialog.dismiss();
                    }
                }
            }, 1000 - rangeTime);
        } else {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        }
    }

    public void addFragment(int containerViewId, Fragment fragment) {
        if (fragment != null && !fragment.isAdded()) {
            getChildFragmentManager().beginTransaction().add(containerViewId, fragment).commitAllowingStateLoss();
        }
    }

    public void showFragment(Fragment fragment) {
        if (fragment != null && fragment.isAdded())
            getChildFragmentManager().beginTransaction().show(fragment).commitAllowingStateLoss();
    }

    public void replaceFragment(int layoutId, Fragment fragment) {
        if (fragment != null)
            getChildFragmentManager().beginTransaction().replace(layoutId, fragment).commitAllowingStateLoss();
    }

    public void hideFragment(Fragment fragment) {
        if (fragment != null && fragment.isAdded())
            getChildFragmentManager().beginTransaction().hide(fragment).commitAllowingStateLoss();
    }
}
